using PP.Basic;
using PP.PPPoker.Game.GJ25;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace PP.PPPoker
{

    public enum Ending
    {
        Bad = 1,
        Normal = 2,
        Good = 3,
    }

    /// <summary>
    /// GJEndingLayer 对象
    /// </summary>
    public class GJEndingLayer : MonoBehaviour
    {
        private const string Tag = nameof(GJEndingLayer);

        // Private Variables Begin
        private AudioPlayTask _audioBGMPlayTask;
        // Private Variables End

        // Static Methods Begin
        public static GJEndingLayer Create(int finalScore)
        {
            var co = UIManager.Instance.ShowView<GJEndingLayer>("Prefab/GJEndingLayer");
            co.InitScore(finalScore);
            return co;
        }
        // Static Methods End

        private void InitScore(int finalScore)
        {
            _finalScoreTextTmp.text = finalScore.ToString();
        }
        
        // Public Methods Begin
        // Public Methods End
        
        // Lifecycle Methods Begin
        protected void Awake()
        {
            InitHier();
            InitPart();
        }

        private void InitPart()
        {
            // Do initialization here...
        }
        
        private void Start()
        {
            var endingType = GameManager.Instance.endingType;
            Debug.Log($"ending : {endingType}");
            _endingImage.sprite =  Resources.Load<Sprite>($"Texture/ending/ending_bg{endingType}");

            if (endingType == 1)
            {
                _audioBGMPlayTask = this.PlayAudio(AudioConfig.BGM_End_1);
            }
            else if (endingType == 2)
            {
                _audioBGMPlayTask = this.PlayAudio(AudioConfig.BGM_End_2);
            }
            else if (endingType == 3)
            {
                _audioBGMPlayTask = this.PlayAudio(AudioConfig.BGM_End_3);
            }
        }
        
        protected void OnDestroy()
        {
            Debug.Log($"{Tag}.OnDispose");

            // Audio.Instance.StopAudio(_audioBGMPlayTask);
            if (_audioBGMPlayTask.AudioSource != null)
                _audioBGMPlayTask.Cancel();

            // Do cleanups here...
            // Stop animations! Cancel global callbacks ...
        }


        // Hier Components Begin        
        private Image _endingImage;
        private TMP_Text _finalScoreTextTmp;
        private Button _leaderBButton;
        private Button _ruleButton;

        private void InitHier()
        {
            _endingImage = transform.Find("_Image_Ending").GetComponent<Image>();
            _finalScoreTextTmp = transform.Find("_TextTmp_FinalScore").GetComponentEx<TMP_Text>();
            _leaderBButton = transform.Find("_Button_LeaderB").GetComponent<Button>();
            _leaderBButton.onClick.AddListener(OnLeaderBButtonClick);

            _ruleButton = transform.Find("_Button_Rule").GetComponent<Button>();
            _ruleButton.onClick.AddListener(OnRuleButtonClick);
        }

        private void OnLeaderBButtonClick()
        {
            GJ25LeaderBoard.Create();
        }


        private void OnRuleButtonClick()
        {
            GJRuleLayer.Create();
        }
        // Hier Components End

    }
}
